﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;

namespace KinectStudio.ModuliTracciamento
{
    /// <summary>
    /// Interaction logic for Scheletro.xaml
    /// </summary>
    public partial class Scheletro : Window
    {
        private KinectSensor sensore;

        public Scheletro()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (KinectSensor possibileSensore in KinectSensor.KinectSensors)
            {
                if (possibileSensore.Status == KinectStatus.Connected)
                {
                    this.sensore = possibileSensore;
                    break;
                }
            }
            if (this.sensore != null)
            {
                this.sensore.SkeletonStream.Enable();
                this.sensore.SkeletonFrameReady += this.RilevamentoScheletroPronto;
                try
                {
                    this.sensore.Start();
                }
                catch (IOException)
                {
                    this.sensore = null;
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.sensore != null)
            {
                this.sensore.Stop();
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuCliccato = (MenuItem)e.OriginalSource;
            switch (menuCliccato.Header.ToString())
            {
                case "Esci":
                    this.Close();
                    break;
            }
        }

        private void RilevamentoScheletroPronto(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] scheletri = new Skeleton[0];

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    scheletri = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(scheletri);
                }
            }
            if (scheletri.Length != 0)
            {
                foreach (Skeleton scheletro in scheletri)
                {
                    if (scheletro.TrackingState == SkeletonTrackingState.Tracked)
                    {
                        impostaPosizionePunti(ui_Testa, scheletro.Joints[JointType.Head]);
                        impostaPosizionePunti(ui_Testa, scheletro.Joints[JointType.HandLeft]);
                        impostaPosizionePunti(ui_Testa, scheletro.Joints[JointType.HandRight]);
                    }
                }
            }
        }

        private void impostaPosizionePunti(FrameworkElement punto, Joint elemento)
        {
            Canvas.SetLeft(punto, elemento.Position.X);
            Canvas.SetTop(punto, elemento.Position.Y);
        }
    }
}
